/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.escuela.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.escuela.bl.AlumnoBlLocal;
import mx.its.dsw2.escuela.modelo.Alumno;

/**
 *
 * @author Maq-8
 */
@ManagedBean
@SessionScoped
public class StudentBean {

    @EJB
    private AlumnoBlLocal alumnoBl;
    private List<Alumno> lista;
    private Alumno student;
    private String accion;
    private List<Alumno> filteredStudents; 

    public StudentBean() {
        student = new Alumno();
    }

    public List<Alumno> getFilteredStudents() {
        return filteredStudents;
    }

    public void setFilteredStudents(List<Alumno> filteredStudents) {
        this.filteredStudents = filteredStudents;
    }

    public Alumno getStudent() {
        return student;
    }

    public void setStudent(Alumno student) {
        this.student = student;
    }

    public List<Alumno> getLista() {
        lista = alumnoBl.getAll();
        return lista;
    }

    //Acciones
    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            alumnoBl.register(student);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                alumnoBl.modify(student);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    alumnoBl.delete(student);
                }
            }
        }
        accion = "";
        return "StudentList";
    }

    public String procesarLlamada() {
        String page = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            page = "StudentCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                page = "StudentList";
            }
        }
        return page;
    }

//Listeners
    public void prepararNuevo(ActionEvent evt) {
        accion = "Nuevo";
        student = new Alumno();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        student = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        student = lista.get(index);
    }

}
